package com.lu.dog.dbrowser.ui.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.lu.adog.adapter.recyclerview.CommonAdapter;
import com.lu.adog.adapter.recyclerview.base.ViewHolder;
import com.lu.adog.base.BaseConfig;
import com.lu.adog.util.Logg;
import com.lu.dog.dbrowser.R;
import com.lu.dog.dbrowser.bean.IconInfo;
import com.lu.dog.dbrowser.db.DBrowserDbHelper;
import com.lu.dog.dbrowser.ui.MainActivity;
import com.lu.dog.dbrowser.ui.WebActivity;

import java.util.List;

/**
 * @author Yue
 * @date 2017/12/25 10:58
 */

public class ItemIconAdapter extends CommonAdapter<ItemIconAdapter.ItemData> {
    public ItemIconAdapter(Context context, int layoutId, List<ItemData> datas) {
        super(context, layoutId, datas);
        fillDataFromDb();
    }

    private void fillDataFromDb() {
        List<IconInfo> iconInfos = DBrowserDbHelper.getInstance(mContext).selectAllIconInfo();
        mDatas.clear();
        for (IconInfo iconInfo : iconInfos) {
            mDatas.add(new ItemData(TYPE_NOTLAST, iconInfo));
        }
        mDatas.add(new ItemData(TYPE_ISLAST, null));

        Logg.w(">>>", mDatas.toString() + "\n");
    }


    @Override
    protected void convert(final ViewHolder holder, final ItemData itemData, final int position) {

        if (itemData.type == TYPE_ISLAST) {
            holder.setText(R.id.tvName, "添加");
            holder.setBackgroundRes(R.id.ivIcon, R.drawable.ic_add);
            holder.setOnLongClickListener(R.id.ivIcon, null);
            holder.setOnClickListener(R.id.ivDelete, null);

            holder.setVisible(R.id.ivDelete, false);
        } else {
            holder.setBackgroundRes(R.id.ivIcon, R.drawable.ic_launcher_background);
            holder.setOnLongClickListener(R.id.ivIcon, new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    showSelectDialog(position);
                    return true;
                }
            });
            holder.setText(R.id.tvName, itemData.iconInfo.getName());
            if (deleteFlag) {
                holder.setVisible(R.id.ivDelete, true);
            } else {
                holder.setVisible(R.id.ivDelete, false);
            }
        }

        holder.setOnClickListener(R.id.ivIcon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemData.type == TYPE_ISLAST) {
                    showAddMenuDialog(position);
                } else {

                    visityWebActivity(itemData.iconInfo.getUrl());
                }
            }
        });

        holder.setOnClickListener(R.id.ivDelete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteARecord(position);
            }
        });
    }

    private void showAddMenuDialog(int position) {
        showEditItemDialog("添加", mDatas.get(position));
    }

    private void showSelectDialog(final int position) {
        new AlertDialog.Builder(mContext)
                .setItems(new String[]{"编辑", "删除", "批量删除"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            showReEditDialog(position);
                        } else if (which == 1) {
                            deleteARecord(position);
                        } else {//删除界面
                            showDeleteMoreView();
                            ((MainActivity) mContext).showBottomBar(BaseConfig.TAG_COMPLETE);
                        }
                        dialog.cancel();
                    }
                }).show();
    }

    private void showDeleteMoreView() {
        setDeleteFlag(true);
        notifyDataSetChanged();

    }

    private void deleteARecord(int position) {
        DBrowserDbHelper.getInstance(mContext).deleteIconInfo(mDatas.get(position).iconInfo);
        mDatas.remove(position);
        notifyDataSetChanged();

    }


    private void visityWebActivity(String url) {
        Intent intent = new Intent(mContext, WebActivity.class);
        intent.putExtra(BaseConfig.KEY_URL, url);
        mContext.startActivity(intent);
    }

    private void showReEditDialog(int position) {
        showEditItemDialog("编辑", mDatas.get(position));
    }

    private void showEditItemDialog(String title, final ItemData itemData) {
        final View layout = LayoutInflater.from(mContext).inflate(R.layout.edit_twoline, null, false);
        final EditText editUrl = (EditText) layout.findViewById(R.id.editUrl);
        final EditText editName = (EditText) layout.findViewById(R.id.editName);
        if (itemData.type == TYPE_NOTLAST && itemData.iconInfo != null) {
            editUrl.setText(itemData.iconInfo.getUrl());
            editName.setText(itemData.iconInfo.getName());
        } else {
            editName.setText("");
            editUrl.setText("");
        }
        new AlertDialog.Builder(mContext)
                .setTitle(title)
                .setView(layout)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String url = ((EditText) layout.findViewById(R.id.editUrl)).getText().toString().trim();
                        String name = ((EditText) layout.findViewById(R.id.editName)).getText().toString().trim();

                        if (TextUtils.isEmpty(url) || TextUtils.isEmpty(name)) {
                            Toast.makeText(mContext, "未填写", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                            return;
                        }

                        long time = System.currentTimeMillis();

                        if (itemData.type == TYPE_NOTLAST && itemData.iconInfo != null) {
                            itemData.iconInfo.setUpdateTime(time);
                            itemData.iconInfo.setUrl(url);
                            itemData.iconInfo.setName(name);
                            DBrowserDbHelper.getInstance(mContext).updateIconInfo(itemData.iconInfo);

                        } else {//添加
                            IconInfo iconInfo = new IconInfo(url, name, null, null, time, time);
                            DBrowserDbHelper.getInstance(mContext).updateIconInfo(iconInfo);
                            addItemData(new ItemData(TYPE_NOTLAST, iconInfo));
                        }

                        notifyDataSetChanged();
                        dialog.cancel();
                    }
                }).show();
    }

//    private void showAddItemDialog(String title) {
//        final View layout = LayoutInflater.from(mContext).inflate(R.layout.edit_twoline, null, false);
//        final EditText editUrl = (EditText) layout.findViewById(R.id.editUrl);
//        final EditText editName = (EditText) layout.findViewById(R.id.editName);
//        editUrl.setText("");
//        editName.setText("");
//
//        new AlertDialog.Builder(mContext)
//                .setTitle(title)
//                .setView(layout)
//                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.cancel();
//                    }
//                })
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        String url = ((EditText) layout.findViewById(R.id.editUrl)).getText().toString().trim();
//                        String name = ((EditText) layout.findViewById(R.id.editName)).getText().toString().trim();
//
//                        if (TextUtils.isEmpty(url) || TextUtils.isEmpty(name)) {
//                            Toast.makeText(mContext, "未填写", Toast.LENGTH_SHORT).show();
//                            dialog.cancel();
//                            return;
//                        }
//                        long time = System.currentTimeMillis();
//                        IconInfo iconInfo = new IconInfo(url, name, null, null, time, time);
//                        DBrowserDbHelper.getInstance(mContext).updateIconInfo(iconInfo);
//
//                        addItemData(new ItemData(TYPE_NOTLAST, iconInfo));
//                        notifyDataSetChanged();
//                        dialog.cancel();
//                    }
//                }).show();
//    }

    public void addItemData(ItemData itemData) {
        mDatas.remove(mDatas.size() - 1);
        mDatas.add(itemData);
        mDatas.add(new ItemData(TYPE_ISLAST, null));

    }

    private boolean deleteFlag = false;

    public void setDeleteFlag(boolean deleteFlag) {
        this.deleteFlag = deleteFlag;
    }

    public void hideDeleteMoreView() {
        setDeleteFlag(false);
        notifyDataSetChanged();
    }

    public boolean isDeleteFlag() {
        return deleteFlag;
    }

    private final static int TYPE_NOTLAST = 0;
    private final static int TYPE_ISLAST = 1;

    public class ItemData {
        public int type;
        public IconInfo iconInfo;

        public ItemData(int type, IconInfo iconInfo) {
            this.type = type;
            this.iconInfo = iconInfo;
        }
    }

    public interface OnLongClickListener {
        void onItemLongClick(View v, int position);
    }

    private OnLongClickListener mOnLongClickListener;

    public void setItemOnLongClickListener(OnLongClickListener onLongClickListener) {
        mOnLongClickListener = mOnLongClickListener;
    }
}
